package iyuwu.PlayerManager;

import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Collection;

public class EventListener implements Listener {
    public void onInventoryClickEvent(InventoryClickEvent e){
        e.setCancelled(true);
        if(e.getWhoClicked().getOpenInventory().getTitle().contains("Player list")) {
            ItemStack ClickItem = e.getWhoClicked().getOpenInventory().getItem(e.getRawSlot());
            ItemMeta meta = ClickItem.getItemMeta();
            String name = meta.getDisplayName();
            Collection<? extends Player> p = Bukkit.getOnlinePlayers();
            for (Player i : p) {
                if (i.getName().equals(name)) {
                    Inventory inv = Bukkit.createInventory(null, 9, "Manager "+name);
                    for (int j = 0; j < 9; j++){
                        inv.setItem(j,new ItemStack(Material.REDSTONE));
                    }
                    e.getWhoClicked().closeInventory();
                    e.getWhoClicked().openInventory(inv);
                    break;
                }
            }
        }
        if(e.getWhoClicked().getOpenInventory().getTitle().contains("Manager")){
            Collection<? extends Player> p = Bukkit.getOnlinePlayers();
            String name=e.getWhoClicked().getOpenInventory().getTitle().substring(8);
            Player tmpPlayer=null;
            for(Player i:p){
                if(i.getName()==name){
                    tmpPlayer=i;
                    break;
                }
            }
            switch (e.getRawSlot()){
                case 0:
                    if(e.getWhoClicked().hasPermission("PlayerManager.admin"))
                        tmpPlayer.kickPlayer("你被管理员踢出了游戏");
                    break;
                case 1:
                    if(e.getWhoClicked().hasPermission("PlayerManager.admin")) {
                        Bukkit.dispatchCommand(e.getWhoClicked(),"ban "+tmpPlayer.getName());
                    }
                    break;
                case 2:
                    if(e.getWhoClicked().hasPermission("PlayerManager.admin")) {
                        Bukkit.dispatchCommand(e.getWhoClicked(),"muteex "+tmpPlayer.getName());
                    }
                case 3:
                    if(e.getWhoClicked().hasPermission("PlayerManager.admin")) {
                        Bukkit.dispatchCommand(e.getWhoClicked(),"unmuteex "+tmpPlayer.getName());
                    }
                case 4:
                    if(e.getWhoClicked().hasPermission("PlayerManager.admin")) {
                        Bukkit.dispatchCommand(e.getWhoClicked(),"kill "+tmpPlayer.getName());
                    }
            }
        }
    }
}
